import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.LinkedList;
import java.util.List;

public class Util {

	/**
	 * @return curator stauts html
	 */
	public static String genHTML() {
		String ret = "";
		String head = "<html><body><table><form  method=\"get\">";
		String end = "</form></table></body></html>";

		String content = "";

		List<CuratorPS> ps = Entry.curatorList;
		for (CuratorPS x : ps) {
			// TextIO.putln(x.getName());
			// TextIO.putln(x.getName()+"update");
			x.updatePID();
			content += x.toHTML();

		}

		ret = head + content + end;
		return ret;

	}

	/**
	 * @return jumo to curator stauts html
	 */
	public static String genToKey() {
		String ret = "";
		String head = "<html><body><table>";

		String end = "</table></body></html>";

		String content = "<meta http-equiv=\"refresh\" content=\"0; URL="
				+ Entry.key + "\">";

		ret = head + content + end;
		return ret;

	}

	/**
	 * @return login screen html
	 */
	public static String genLogin() {
		String ret = "";
		String head = "<html><body><table>";
		String end = "</table></body></html>";

		String content = "<form method=\"GET\" action=\"login\"><div align=\"center\">Password: <input type=\"password\" name=\"password\" size=\"15\" /><br /><p><input type=\"submit\" value=\"Login\" /></p></div></form>";

		ret = head + content + end;
		return ret;

	}

	/**
	 * run the command
	 */
	public static void exec(String script) {
		System.out.println("try to exec " + script);
		try {

			String[] command = new String[3];

			command[0] = "sh";

			command[1] = "-c";
			command[2] = script;
			Process process;

			process = Runtime.getRuntime().exec(command);
			InputStreamReader ir = new InputStreamReader(
					process.getInputStream());
			LineNumberReader input = new LineNumberReader(ir);
			String line;
			while ((line = input.readLine()) != null) {
				try {
					System.out.println(line + "\n");
				} catch (Exception e) {
					System.err.println("Error: " + e.getMessage());
				}
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}

	}

	/**
	 * kill an process
	 */
	public static void killPS(int PID) {

		try {

			String[] command = new String[3];

			command[0] = "sh";

			command[1] = "-c";
			command[2] = "kill " + PID;
			Process process;

			process = Runtime.getRuntime().exec(command);
			InputStreamReader ir = new InputStreamReader(
					process.getInputStream());
			LineNumberReader input = new LineNumberReader(ir);

			String line;
			while ((line = input.readLine()) != null) {
				try {
					System.out.println(line + "\n");
				} catch (Exception e) {
					System.err.println("Error: " + e.getMessage());
				}
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}

	}

	/**
	 * Get an PID (using jps for java,ps for other)
	 */
	public static int getPID(String name, boolean java) {
		int ret = 0;
		try {

			String[] command = new String[3];

			command[0] = "sh";

			command[1] = "-c";
			if (java)
				command[2] = "jps |grep " + name;
			else
				command[2] = "ps -e |grep " + name;

			if (name.contains("NEREXT")) {
				command[2]="jps -v |grep "+name;
			}

			Process process;
			// TextIO.putln(command[2]);

			process = Runtime.getRuntime().exec(command);
			InputStreamReader ir = new InputStreamReader(
					process.getInputStream());
			LineNumberReader input = new LineNumberReader(ir);

			String line;
			while ((line = input.readLine()) != null) {
				try {
					// TextIO.putln(line);
					if (line.contains("grep " + name))
						continue;

					ret = readInt(line);
				} catch (Exception e) {
					System.err.println("Error: " + e.getMessage());
				}
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * Get a list of curator tools, read from config file
	 */
	public static List<CuratorPS> getCuratorList() {
		List<CuratorPS> ret = new LinkedList<CuratorPS>();

		String line = "";
		TextIO.readFile("configs/NonJavaServer.config");
		if (!TextIO.eof())
			line = TextIO.getln();
		// TextIO.putln(line);
		while (!TextIO.eof() || line != null) {
			if (line.startsWith("#") == false) {
				ret.add(new CuratorPS(line, false));
				// TextIO.putln(line);
			}
			if (TextIO.eof())
				break;
			line = TextIO.getln();
		}
		//
		TextIO.readFile("configs/JavaServer.config");
		if (!TextIO.eof())
			line = TextIO.getln();
		// TextIO.putln(line);
		while (!TextIO.eof() || line != null) {
			if (line.startsWith("#") == false) {
				ret.add(new CuratorPS(line, true));
				// TextIO.putln(line);
			}
			if (TextIO.eof())
				break;
			line = TextIO.getln();
		}
		return ret;
	}

	public static List<CuratorPS> getPS() {
		List<CuratorPS> ret = new LinkedList<CuratorPS>();
		try {

			String[] command = new String[3];

			command[0] = "sh";

			command[1] = "-c";
			command[2] = "jps";
			Process process;

			process = Runtime.getRuntime().exec(command);
			InputStreamReader ir = new InputStreamReader(
					process.getInputStream());
			LineNumberReader input = new LineNumberReader(ir);

			String line;
			while ((line = input.readLine()) != null) {
				try {
					ret.add(new CuratorPS(line));

				} catch (Exception e) {
					System.err.println("Error: " + e.getMessage());
				}
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}

		return ret;
	}

	/**
	 * Get password in config file
	 */
	public static String getPassWD() {
		// TODO Auto-generated method stub
		TextIO.readFile("configs/MonitorPassWD.config");
		String ret = "";
		try {
			ret = TextIO.getln();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
			System.exit(1);
		}
		return ret;

	}

	/**
	 * read the first integer in an string.
	 * 
	 * used in getPID function.
	 * 
	 */
	public static int readInt(String input) {
		input = input.trim();

		char[] squ = input.toCharArray();

		String test = "0123456789";
		boolean end = false;
		String out = "";
		for (int i = 0; i < squ.length; i++) {
			if (test.contains(squ[i] + "") == false) {
				end = true;
			}

			if (!end) {
				out += squ[i] + "";
			}

		}

		if (out.length() == 0)
			return 0;
		else
			return Integer.parseInt(out);

	}

}
